var api = {
  dateFormat: function(time, fmt) {
    if(!time) {
      return "";
    }
    var d = new Date(time);
    var o = {
      "M+": d.getMonth() + 1, //月份   
      "d+": d.getDate(), //日   
      "h+": d.getHours(), //小时   
      "m+": d.getMinutes(), //分   
      "s+": d.getSeconds(), //秒   
      "q+": Math.floor((d.getMonth() + 3) / 3), //季度   
      "S": d.getMilliseconds() //毫秒   
    };
    if(/(y+)/.test(fmt))
      fmt = fmt.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
    for(var k in o)
      if(new RegExp("(" + k + ")").test(fmt))
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
  },
  vueDataFormat: function(arg) {
    return JSON.parse(JSON.stringify(arg));
  },
  getUrlParams: function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if(r) {
      return decodeURI(r[2]);
    } else {
      return null;
    }
  },
  dataUrl: "http://admin.poolgpu.com:5000/",
  get: function(obj) {
    obj.type = "GET";
    this.ajax(obj);
  },
  post: function(obj) {
    obj.type = "POST";
    this.ajax(obj);
  },
  del: function(obj) {
    obj.type="DELETE";
    this.ajax(obj);
  },
  ajax: function(obj) {
    $.ajax({
      type: obj.type,
      url: this.dataUrl + obj.url,
      xhrFields: {
        withCredentials: true
      },
      data: obj.data,
      dataType: "json",
      success: function(data) {
        if(typeof data == "string") {
          data = JSON.parse(data)
        }
        if(data.code == (-3)) {
          alert("请登录！");
          window.location.href = "/#/login";
        }
        obj.callback(data)
      },
      error: function() {
        if(obj.failback) {
          obj.failback();
        }
      }
    });
  },
  getReadableHashRateString: function(hashrate, unit) {
    unit = unit || false
    hashrate = (hashrate * 2);
    if(hashrate < 1000000) {
      if (unit) {
        return(Math.round(hashrate / 1000) / 1000).toFixed(2) + ' Sol/s';
      } else {
        return(Math.round(hashrate / 1000) / 1000).toFixed(2);
      }
    }
    if (unit) {
      var byteUnits = [' Sol/s', ' KSol/s', ' MSol/s', ' GSol/s', ' TSol/s', ' PSol/s'];
      var i = Math.floor((Math.log(hashrate / 1000) / Math.log(1000)) - 1);
      hashrate = (hashrate / 1000) / Math.pow(1000, i + 1);
      return hashrate.toFixed(2) + byteUnits[i];
    } else {
      hashrate = (hashrate / 1000) / Math.pow(1000, 1);
      return hashrate.toFixed(2);
    }
  },
  getCookie: function(c_name) {
    if (document.cookie.length > 0) {
      var c_start;
      var c_end;
      c_start = document.cookie.indexOf(c_name + "=");
      if (c_start != -1) {
          c_start = c_start + c_name.length + 1;
          c_end = document.cookie.indexOf(";", c_start);
          if (c_end == -1) c_end = document.cookie.length;
          return unescape(document.cookie.substring(c_start, c_end));
      }
    }
    return "";
  },
  setCookie: function(c_name, value, expiredays) {
    var exdate = new Date()
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) +
        ((expiredays == null) ? "" : "; expires=" + exdate.toGMTString());
  },
  clearCookie: function(arr) {
    for(var i = 0; i < arr.length; i ++) {
      this.setCookie(arr[i], "", -1);
    }
  }
}
export default api;
